<?php


namespace app\mobile\controller;


use think\cache\driver\Redis;
use think\Controller;
use think\Db;
use think\facade\Config;

class Chapter extends Controller
{
    public function read(){
        $book_id = input('book_id');
        $chapter_index = input('chapter_index');
        $book = db('book')->find($book_id);
        if(empty($chapter_index)){
            $chapter = db("chapter")->where('state',1)->order("index asc")->where('book_id',$book_id)->find();
        }else{
            $chapter = db("chapter")->where('state',1)->where('book_id',$book_id)->where('index',$chapter_index)->find();
        }
        if(empty($chapter)){
            $this->assign('end',1);
        }
        /*dump($chapter);*/
        //判断当前章节是否是VIP
        if($chapter['is_free'] == 1){
            $member_id = session('member_id');
            $buy = db('buy')->where('m_id',$member_id)->where('c_id',$chapter['id'])->find();
            if(empty($buy)){
                $member = db('member')->find($member_id);
                $money = floor($chapter['size']/Config::get('custom.money_words'));//根据章节字数计算出需要多少书币
                $content = null;
                if(!empty($member_id)){ //判断用户是否登录
                    if($member['is_auto'] == 1){ //自动购买下一章节
                        if($member['money'] < $money){ //书币不足
                            $this->assign('money',-1);
                        }else{
                            $content = $this->executeMoneyContent($member_id,$chapter['content_id'],$money,$chapter['id']);
                            $this->assign('content',$content);
                        }
                    }else{ //不是自动购买下一章节
                        $content = db('content')->where('id',$chapter['content_id'])->find();
                        $this->assign('is_auto',-1);
                        $content['content'] = mb_substr($content['content'],0,100);
                        $this->assign('content',$content);
                        $this->assign('money',$money);
                    }
                    $this->assign('member_id',$member_id);
                }else{
                    $this->assign('member_id', -1);
                }
            }else{
                $this->assign('member_id',0);
                $content = db('content')->where('id',$chapter['content_id'])->find();
            }
        }else{
            $this->assign('member_id',0);
            $content = db('content')->where('id',$chapter['content_id'])->find();
        }
//        $content = db('content')->where('id',$chapter['content_id'])->find();
        $chapter_total = db('chapter')->where('book_id',$book_id)->count('id');
        /*if($chapter_total < $chapter_index){
            $this->assign('end',1);
        }*/
        $this->chapter_pv($chapter,$book);//章节点击增加
        $this->assign([
            'book' => $book,
            'chapter' => $chapter,
            'chapter_index' =>$chapter['index'],
            'content' => $content,
            'chapter_total' => $chapter_total
        ]);
        return view('read');
    }
    // 章节点击量
    public function chapter_pv($chapter,$book)
    {
        $ip = request()->ip();
        $key = $ip . ':' . $chapter['id'];
        $redis = new Redis();
        $val = $redis->get($key);
        if (!$val) {
            $redis->set($key, $chapter['id'], 300);
            db('chapter')->where('id', $chapter['id'])->update(['pv' => $chapter['pv'] + 1]);
            db('book')->where('id', $book['id'])->update(['chapter_pv' => $book['chapter_pv'] + 1]);
        }
    }
    //扣除用户余额 查询出来内容 添加购买记录
    public function executeMoneyContent($member_id,$content_id,$money,$chapter_id)
    {
        $result = db('member')->where('id',$member_id)->setDec('money', $money);
        if(!$result) return $content = null;
        db('buy')->insert(['m_id' => $member_id,'c_id' => $chapter_id,'money' => $money]);
        $content = db('content')->find($content_id);
        return $content;
    }
    //章节购买
    public function chapterBuy(){
        $chapter_id = input('chapter_id');
        $member_id = session('member_id');
        $money = input('money');
//        return $money.'--'.$member_id.'---'.$chapter_id;
        if(empty($member_id)) return ['state'=> -1,'msg' => '请先登录!!!'];
        Db::startTrans();
        try {
            $member = db('member')->where('id',$member_id)->setDec('money', $money);
            $buy = db('buy')->insert(['m_id' => $member_id,'c_id' => $chapter_id,'money' => $money]);
            if(!$member || !$buy){
                Db::rollback();die;
            }
            // 提交事务
            Db::commit();
            return ['state' => 200,'msg' => '购买成功'];
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return ['state' => -1,'msg' => '购买失败'];
        }

    }
}